import React from 'react';
import { Button } from 'antd';
import { saveAs } from 'file-saver';
import * as XLSX from 'xlsx';

interface FileProps {
  fileContent: { [key: string]: string }[];
}

const DownloadExcel: React.FC<FileProps> = ({ fileContent }) => {
  const handleDownload = () => {
    // 创建一个工作簿
    const workbook = XLSX.utils.book_new();

    // 创建一个工作表
    const worksheet = XLSX.utils.json_to_sheet(fileContent);

    // 将工作表添加到工作簿
    XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1');

    // 将工作簿转换为二进制字符串
    const excelBuffer = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });

    // 创建一个 Blob 对象
    const blob = new Blob([excelBuffer], { type: 'application/octet-stream' });

    // 使用 file-saver 下载文件
    saveAs(blob, '示例文件.xlsx');
  };

  return (
    <Button type="link" onClick={handleDownload}>
      下载示例文件
    </Button>
  );
};

export default DownloadExcel;
